<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>gltf零部件控制(运载火箭) | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,jquery.range,font-awesome,bootstrap,layer,haoutil,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
    <style>
      .infoview td {
        padding-left: 5px;
        padding-top: 5px;
      }
    </style>
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <div id="toolbar" class="infoview">
      <div>
        连接部件:
        <select class="cesium-button" data-bind="options: articulations,optionsText: 'name',value: selectedArticulation"></select>
      </div>
      <table>
        <tbody data-bind="foreach: stages">
          <tr>
            <td data-bind="text: name"></td>
            <td>
              <input type="range"
                      min="-3" max="3"
                      step="0.01"
                      data-bind="value: current, valueUpdate: 'input',attr: {min: minimum,max: maximum}" />
            </td>
            <td>
              <input type="text" size="2" data-bind="value: currentText" />
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      //汉化属性名称
      var objNames = {
        //属性分组
        LaunchVehicle: "火箭整体",
        Fairing: "整流罩",

        UpperStage: "二级部分",
        UpperStageEngines: "二级发动机",
        UpperStageFlames: "二级火焰",

        InterstageAdapter: "级间段",

        Booster: "一级部分",
        BoosterEngines: "一级发动机",
        BoosterFlames: "一级火焰",

        SRBs: "助推器",
        SRBFlames: "助推器火焰",

        //属性
        MoveX: "X方向移动",
        MoveY: "Y方向移动",
        MoveZ: "Z方向移动",
        Yaw: "Yaw角度",
        Pitch: "Pitch角度",
        Roll: "Roll角度",
        Size: "大小",
        Separate: "分离",
        Drop: "下降",
        Open: "打开",
        Rotate: "旋转",
      };

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {});

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        //固定光照，避免gltf模型随时间存在亮度不一致。
        map.fixedLight = true;

        //绑定到UI界面控制参数
        var viewModel = {
          articulations: [],
          stages: [],
          selectedArticulation: undefined,
        };
        Cesium.knockout.track(viewModel);
        Cesium.knockout.getObservable(viewModel, "selectedArticulation").subscribe(function (newArticulation) {
          viewModel.stages = newArticulation.stages;
        });

        var toolbar = document.getElementById("toolbar");
        Cesium.knockout.applyBindings(viewModel, toolbar);

        //创建Graphic图层
        var graphicLayer = new mars3d.layer.GraphicLayer();
        map.addLayer(graphicLayer);

        var primitive = new mars3d.graphic.ModelPrimitive({
          position: [113.693, 31.243, 220000],
          style: {
            url: "//data.mars3d.cn/gltf/sample/launchvehicle/launchvehicle.glb",
            scale: 20,
            minimumPixelSize: 128,
            heading: 0,
          },
        });
        graphicLayer.addGraphic(primitive);

        //gltf模型加载完成事件
        primitive.on(mars3d.EventType.load, (event) => {
          var model = event.model;

          //缩放区域到模型所在位置
          var controller = map.scene.screenSpaceCameraController;
          var r = 2.0 * Math.max(model.boundingSphere.radius, map.camera.frustum.near);
          controller.minimumZoomDistance = r * 0.2;

          var center = Cesium.Matrix4.multiplyByPoint(model.modelMatrix, Cesium.Cartesian3.ZERO, new Cesium.Cartesian3());
          var heading = Cesium.Math.toRadians(0.0);
          var pitch = Cesium.Math.toRadians(-10.0);
          map.camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, r * 1.5));

          //设置参数效果
          model.setArticulationStage("LaunchVehicle Pitch", -60); //火箭整体Pitch角度
          model.setArticulationStage("SRBFlames Size", 1); //助推器火焰大小
          model.applyArticulations();

          //递归取参数
          viewModel.articulations = Object.keys(model._runtime.articulationsByName).map(function (articulationName) {
            var name = objNames[articulationName] || articulationName;
            //属性分组
            return {
              name: name,
              stages: model._runtime.articulationsByName[articulationName].stages.map(function (stage) {
                //属性
                var stageModel = {
                  name: objNames[stage.name] || stage.name,
                  minimum: stage.minimumValue,
                  maximum: stage.maximumValue,
                  current: stage.currentValue,
                };
                Cesium.knockout.track(stageModel);
                Cesium.knockout.defineProperty(stageModel, "currentText", {
                  get: function () {
                    return stageModel.current.toString();
                  },
                  set: function (value) {
                    // coerce values to number
                    stageModel.current = +value;
                  },
                });
                Cesium.knockout.getObservable(stageModel, "current").subscribe(function (newValue) {
                  var _name = articulationName + " " + stage.name;
                  var _val = Number(stageModel.current);

                  model.setArticulationStage(_name, _val);
                  model.applyArticulations();

                  console.log(
                    "model.setArticulationStage('" + _name + "', " + _val + "); //" + (objNames[articulationName] || "") + (objNames[stage.name] || "")
                  );
                });
                return stageModel;
              }),
            };
          });
          viewModel.selectedArticulation = viewModel.articulations[0];
        });
      }
    </script>
  </body>
</html>
